/*
 * Copyright (c) 2007
 *	Alberto Magni, Maggioni Marcello
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

package Database.XMLManager;

import exceptions.DatabaseException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *  This class is the representation of a XML file storing configuration informations.
 * 
 * @author Marcello Maggioni
 */
public class XMLConfigurationFile extends XMLFile {
    
    /**
     * Constructs an instance of XMLConfigurationFile
     * 
     * @param path The path of the XML file
     * @throws exceptions.DatabaseException
     * @throws javax.xml.parsers.ParserConfigurationException
     * @throws javax.xml.transform.TransformerConfigurationException
     */
    protected XMLConfigurationFile(String path) throws DatabaseException, ParserConfigurationException, TransformerConfigurationException {
        super(path);
        if (!exists())
            init();
    }
   /**
    * This method is used to set the server in the config file
    * 
    * @param server A string with the address of the server
    * @throws exceptions.DatabaseException
    */ 
    protected void setServer(String server) throws DatabaseException {
        Document doc = readFile();
        Element root = doc.getDocumentElement();
        NodeList nl = root.getElementsByTagName("server");
        if (nl.getLength() == 1) {
            Element el = (Element) nl.item(0);
            el.setAttribute("address", server);
        }
        else {
            Element el = doc.createElement("server");
            el.setAttribute("address", server);
            root.appendChild(el);
        }
        writeFile(doc);
    }
    /**
     * This method is used to set the Email address in the configuration file
     * 
     * @param email A string containing the email address
     * @throws exceptions.DatabaseException
     */
    protected void setEMailAddress(String email) throws DatabaseException {
        Document doc = readFile();
        Element root = doc.getDocumentElement();
        NodeList nl = root.getElementsByTagName("email");
        if (nl.getLength() == 1) {
            Element el = (Element) nl.item(0);
            el.setAttribute("address", email);
        } else {
            Element el = doc.createElement("email");
            el.setAttribute("address", email);
            root.appendChild(el);
        }
        writeFile(doc);
    }
    /**
     * This method returns the server stored in the configuration file
     * 
     * @return The server stored in the configuration file
     * @throws exceptions.DatabaseException
     */
    protected String getServer() throws DatabaseException {
        Document doc = readFile();
        Element root = doc.getDocumentElement();
        NodeList nl = root.getElementsByTagName("server");
        if (nl.getLength() == 1) {
            Element el = (Element) nl.item(0);
            return el.getAttribute("address");
        }
        else
            return null;
    }
    
    /**
     * Returns the email address stored in the configuration file
     * 
     * @return The email address stored in the configuration file
     * @throws exceptions.DatabaseException
     */
    protected String getEMailAddress() throws DatabaseException {
        Document doc = readFile();
        Element root = doc.getDocumentElement();
        NodeList nl = root.getElementsByTagName("email");
        if (nl.getLength() == 1) {
            Element el = (Element) nl.item(0);
            return el.getAttribute("address");
        }
        
        return null;
    }
    /**
     * This method is used to set the spam check flag in the configuration file
     * 
     * @param value The boolean value of the flag
     * @throws exceptions.DatabaseException
     */
    protected void setSpamCheck(boolean value) throws DatabaseException {
        Document doc = readFile();
        Element root = doc.getDocumentElement();
        NodeList nl = root.getElementsByTagName("spamcheck");
        if (nl.getLength() == 1) {
            Element el = (Element) nl.item(0);
            if (value)
                el.setAttribute("enabled", "true");
            else
                el.setAttribute("enabled", "false");
        } else {
            Element el = doc.createElement("spamcheck");
            if (value)
                el.setAttribute("enabled", "true");
            else
                el.setAttribute("enabled", "false");
            root.appendChild(el);
        }
        writeFile(doc);
    }
    /**
     * This method returns the value of the spam check flag
     * 
     * @return The value of the spamcheck flag
     * @throws exceptions.DatabaseException
     */
    protected boolean getSpamCheck() throws DatabaseException {
        Document doc = readFile();
        Element root = doc.getDocumentElement();
        NodeList nl = root.getElementsByTagName("spamcheck");
        int num = nl.getLength();
        if (nl.getLength() == 1) {
            Element el = (Element) nl.item(0);
            if (el.getAttribute("enabled").equals("true"))
                return true;
        }
        
        return false;
    }
    /** 
     * This method is used to set the server authentication flag stored in the configuration file.
     * 
     * @param value The boolean value of the flag
     * @throws exceptions.DatabaseException
     */
    protected void setServerAuthMode(boolean value) throws DatabaseException {
        Document doc = readFile();
        Element root = doc.getDocumentElement();
        NodeList nl = root.getElementsByTagName("srvauth");
        if (nl.getLength() == 1) {
            Element el = (Element) nl.item(0);
            if (value)
                el.setAttribute("enabled", "true");
            else
                el.setAttribute("enabled", "false");
        } else {
            Element el = doc.createElement("srvauth");
            if (value)
                el.setAttribute("enabled", "true");
            else
                el.setAttribute("enabled", "false");
            root.appendChild(el);
        }
        writeFile(doc);
    }
/**
  * This method returns the value of the server authentication flag stored in the configuration flag
  * 
  * @return The value of the flag
  * @throws exceptions.DatabaseException
  */
    protected boolean getServerAuthMode() throws DatabaseException {
        Document doc = readFile();
        Element root = doc.getDocumentElement();
        NodeList nl = root.getElementsByTagName("srvauth");
        int num = nl.getLength();
        if (nl.getLength() == 1) {
            Element el = (Element) nl.item(0);
            if (el.getAttribute("enabled").equals("true"))
                return true;
        }
        
        return false;
    }    
    
    private void init() throws DatabaseException {
        try {
            super.initialize();
            Document d = readFile();
            Element messages = d.createElement("configuration");
            d.appendChild(messages);
            writeFile(d);
        } catch (Exception ex) {
            throw new DatabaseException(ex.getMessage());
        }
    }
    
}
